package com.jielihaofeng.site.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jielihaofeng.site.article.dao.ArticleDao;
import com.jielihaofeng.site.article.entity.ArticleEntity;
import com.jielihaofeng.site.article.service.ArticleService;
import com.jielihaofeng.site.article.vo.ArticleVO;
import lombok.AllArgsConstructor;
import com.jielihaofeng.framework.common.page.PageResult;
import com.jielihaofeng.framework.common.service.impl.BaseServiceImpl;
import com.jielihaofeng.site.article.convert.ArticleConvert;
import com.jielihaofeng.site.article.query.ArticleQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 个人网站-文章管理
 *
 * @author 借力好风 949394048@qq.com
 * @since 1.0.0 2022-07-21
 */
@Service
@AllArgsConstructor
public class ArticleServiceImpl extends BaseServiceImpl<ArticleDao, ArticleEntity> implements ArticleService {

    @Override
    public PageResult<ArticleVO> page(ArticleQuery query) {
        IPage<ArticleEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(ArticleConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private QueryWrapper<ArticleEntity> getWrapper(ArticleQuery query) {
        QueryWrapper<ArticleEntity> wrapper = new QueryWrapper<>();

        return wrapper;
    }

    @Override
    public Long save(ArticleVO vo) {
        ArticleEntity entity = ArticleConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
        return entity.getId();
    }

    @Override
    public Long update(ArticleVO vo) {
        ArticleEntity entity = ArticleConvert.INSTANCE.convert(vo);

        updateById(entity);
        return entity.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}